package com.vote.www.controller;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import com.sun.xml.internal.xsom.impl.scd.Iterators.Map;
import com.vote.www.core.page.Pagination;
import com.vote.www.domain.Czjdataresource;
import com.vote.www.service.CzjdatasuService;

@Controller
public class CzjdatasuController {

	private static final Logger log = LoggerFactory
			.getLogger(CzjdatasuController.class);
	
	@Autowired
	private CzjdatasuService czjdatasuService;

	/***
	 * 
	 * 方法名：list 显示所有模板数据 创建人：cy 时间：2017年5月16日-下午4:55:54
	 * 
	 * @param request
	 * @param response
	 * @return String
	 * @exception
	 * @since 1.0.0
	 */
	@RequestMapping("listDatasu")
	public String list(Integer pageNo, Integer pageSize,HttpServletRequest request,
			HttpServletResponse response, ModelMap model) throws Exception {

		 try {
			if (pageNo == null) {
			        pageNo = 1;
			    }
			    if (pageSize == null) {
			        pageSize = 0;
			    }
			    Pagination pagination = czjdatasuService.list(pageNo.intValue(), pageSize.intValue());
			    model.addAttribute("pagination", pagination);
		} catch (Exception e) {
			e.printStackTrace();
			log.error("显示模板错误", e);
			
		}
	        
		return "listDatasu";
	}

	/**
	 * 
	 * 方法名：updateOrAddPre 新增或修改前 创建人：RUI 时间：2017年5月16日-下午4:59:49
	 * 
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 * @throws Exception
	 *             String
	 * @exception
	 * @since 1.0.0
	 */
	@RequestMapping("updateOrAddPre")
	public String updateOrAddPre(String dataresourceId, HttpServletRequest request,
			HttpServletResponse response, ModelMap model) throws Exception {
		
		try {
			
			Czjdataresource czjdataresource= czjdatasuService.findById(dataresourceId);
			model.addAttribute("msg",request.getParameter("msg"));
			model.addAttribute("czjdataresource", czjdataresource);
		} catch (Exception e) {
			e.printStackTrace();
			log.error("error", e);
		}
		
		return "updateOrAddPre";
	}

	/**
	 * 
	 * 方法名：updateOrAdd 修改或新增
	 * 创建人：RUI
	 * 时间：2017年5月16日-下午5:01:53 
	 * @param request
	 * @param response
	 * @param model
	 * @throws Exception void
	 * @exception 
	 * @since  1.0.0
	 */
	@RequestMapping("updateOrAdd")
	public String updateOrAdd(HttpServletRequest request,
			HttpServletResponse response,Czjdataresource czjdataresource,  ModelMap model) throws Exception {

		try {
			czjdataresource.setUpdatetime(new Date());
			czjdatasuService.updateCzjdatasu(czjdataresource);
		} catch (Exception e) {
			 model.addAttribute("msg", "保存失败");
			e.printStackTrace();
			log.error("error",e);
		}
		 return "redirect:updateOrAddPre.do?dataresourceId="+czjdataresource.getDataresourceId();
	}
}
